from jsonrpc import dispatcher

from common import RpcResponse, RpcStatus
from common.exception import ServiceException
from service_sms import WebHookService


class RegisterMethod:
    """
    注册rpc方法
    """

    @staticmethod
    @dispatcher.add_method
    def send_win_xin_text_msg(webhook_url, msg):
        """
        发送文本消息
        :param webhook_url: Webhook地址
        :param msg: 消息内容
        """
        try:
            msg_api = WebHookService.WebHookMessage()
            msg_api.send_win_xin_text(webhook_url, msg)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "文本消息已发送到微信")

    @staticmethod
    @dispatcher.add_method
    def sent_win_xin_markdown_msg(webhook_url, msg):
        """
        发送Markdown消息
        :param webhook_url: Webhook地址
        :param msg: 消息内容
        """
        try:
            msg_api = WebHookService.WebHookMessage()
            msg_api.send_win_xin_markdown(webhook_url, msg)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "Markdown消息已发送到微信")
